// 消息确认弹窗
import * as Blockly from 'blockly/core'
// 1-----消息确认弹窗----------------------------------
const messageBoxJson = {
  type: 'message_box',
  message0: '消息确认提示 %1',
  args0: [
    // 动态生成选项
    {
      'type': 'field_input',
      'name': 'MESSAGE_INPUT',
      check: 'String',
      text: '提示内容'
    }
  ],
  style: 'tool_blocks',
  previousStatement: true,
  nextStatement: true,
  // inputsInline: true,
  tooltip: '',
  helpUrl: '',
  mutator: 'message_mutator'
}

// 创建积木
Blockly.Blocks['message_box'] = {
  init: function () {
    this.jsonInit(messageBoxJson)
  }
}

const mixinObj = {
  mutationToDom: function () {
    var container = Blockly.utils.xml.createElement('mutation')
    container.setAttribute('confirmed', this.confirmCount_)
    container.setAttribute('cancel', this.cancelCount_)
    return container
  },
  domToMutation: function (xmlElement) {
    this.confirmCount_ = parseInt(xmlElement.getAttribute('confirmed'), 10) || 0
    this.cancelCount_ = parseInt(xmlElement.getAttribute('cancel'), 10) || 0
    this.updateShape_()
  },
  updateShape_: function () {
    if (this.confirmCount_) {
      if (!this.getInput('message_confirmed')) {
        this.appendValueInput('message_confirmed').setCheck('callback_function_expression').appendField('确认回调')
      }
    } else {
      if (this.getInput('message_confirmed')) {
        this.removeInput('message_confirmed')
      }
    }
    if (this.cancelCount_) {
      if (!this.getInput('message_cancel')) {
        this.appendValueInput('message_cancel').setCheck('callback_function_expression').appendField('取消回调')
      }
    } else {
      if (this.getInput('message_cancel')) {
        this.removeInput('message_cancel')
      }
    }
  }
}
const modalHelpFun = function () {
  this.confirmCount_ = 1
  this.cancelCount_ = 1
  this.updateShape_()
}
Blockly.Extensions.registerMutator('message_mutator',
  mixinObj, modalHelpFun,
  [ 'message_confirmed', 'message_cancel'])

Blockly.JavaScript['message_box'] = function (block) {
  const text = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE_INPUT'))
  let code = `context.MessageBox.confirm(${text}, '提示',{
    showCancelButton: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
    })`
  if (block.confirmCount_) {
    const onOpen = Blockly.JavaScript.valueToCode(block, 'message_confirmed',
      Blockly.JavaScript.ORDER_COMMA) || '() => {}'
    code = code + `.then(${onOpen})`
  }
  if (block.cancelCount_) {
    const onClose = Blockly.JavaScript.valueToCode(block, 'message_cancel',
      Blockly.JavaScript.ORDER_COMMA) || '() => {}'
    code = code + `.catch( ${onClose})`
  }
  code = code + `\n`
  return code
}

// ----------------------mutator中的组件---------------------------------------

const confirmJson = {
  'type': 'message_confirmed',
  'message0': '确定回调 %1',
  'args0': [
    {
      'type': 'input_value',
      'name': 'message_confirmed'
    }
  ],
  'previousStatement': null,
  'nextStatement': null,
  'enableContextMenu': false,
  'style': 'action_blocks',
  'tooltip': ''
}

// 创建积木
Blockly.Blocks['message_confirmed'] = {
  init: function () {
    this.jsonInit(confirmJson)
  }
}

const cancelJson = {
  'type': 'message_cancel',
  'message0': '取消回调 %1',
  'args0': [
    {
      'type': 'input_value',
      'name': 'message_cancel'
    }
  ],
  'previousStatement': null,
  'nextStatement': null,
  'enableContextMenu': false,
  'style': 'action_blocks',
  'tooltip': ''
}

// 创建积木
Blockly.Blocks['message_cancel'] = {
  init: function () {
    this.jsonInit(cancelJson)
  }
}
